package com.smartsaas.ugclivii.cd;

import com.smartsaas.ugclivii.ui.UiContext;
import nxopen.NXException;
import nxopen.blockstyler.Node;
import nxopen.blockstyler.Tree;

import java.rmi.RemoteException;

public class DesignTreeStateChangeHandler implements Tree.OnStateChangeCallback {
    @Override
    public void onStateChangeCallback(Tree tree, Node node, int i) throws NXException, RemoteException {
        int state = node.getState()==1?2:1;
        node.setState(state);
        if (node.getColumnDisplayText(0).equals("All")){
            Node childNode = node.firstChildNode();
            if (childNode!=null){
                childNode.setState(state);
                setNextNodeState(node,childNode,state);
            }
        }
    }

    private void setNextNodeState(Node root,Node node, int state) throws NXException, RemoteException {
        Node next = node.nextNode();
        if (next==null||next.parentNode()!=root){
            return;
        }
        next.setState(state);
        setNextNodeState(root,next,state);
    }
}
